package com.ruoyi.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.domain.Ai;
import com.ruoyi.util.AsyncUploadService;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.RiverView;
import com.ruoyi.service.IRiverViewService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * river_viewController
 * 
 * @author ya
 * @date 2025-06-07
 */
@RestController
@RequestMapping("/river_view/river_view")
public class RiverViewController extends BaseController
{
    @Autowired
    private IRiverViewService riverViewService;

    @Autowired
    private FileStorageService fileStorageService;//注入实列

    @Autowired
    private AsyncUploadService asyncUploadService;


    /**
     * 查询river_view列表
     */
    /**
     * 获取河流视图列表（带分页功能）
     *
     * @param riverView 河流视图查询条件
     * @return 分页数据
     */
    @GetMapping("/list")
    public TableDataInfo list(RiverView riverView)
    {
        // RuoYi 会自动从请求参数中获取 pageNum 和 pageSize
        startPage();

        // 查询河流视图列表数据
        List<RiverView> list = riverViewService.selectRiverViewList(riverView);

        // 返回分页数据表格信息
        return getDataTable(list);
    }
    @Anonymous
    @GetMapping("/list2")
    public TableDataInfo list2(RiverView riverView)
    {
        startPage();
        List<RiverView> list = riverViewService.selectRiverViewList(riverView);
        return getDataTable(list);
    }

    /**
     * 导出river_view列表
     */
    @Anonymous
    @Log(title = "river_view", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiverView riverView)
    {
        List<RiverView> list = riverViewService.selectRiverViewList(riverView);
        ExcelUtil<RiverView> util = new ExcelUtil<RiverView>(RiverView.class);
        util.exportExcel(response, list, "river_view数据");
    }

    /**
     * 获取river_view详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(riverViewService.selectRiverViewById(id));
    }

    /**
     * 新增river_view
     * 异步上传视频和封面图
     */
    @PostMapping("/add/{id}")
    public AjaxResult add(@PathVariable Long id,
                          @RequestParam(value = "name", required = false) String name,
                          @RequestParam(value = "video", required = false) MultipartFile video,
                          @RequestParam(value = "cover_img", required = false) MultipartFile cover_img) {

        RiverView river_View = new RiverView();
        river_View.setId(id);

        if (name != null) river_View.setName(name);

        // 异步上传视频
        if (video != null && !video.isEmpty()) {
            try {
                byte[] videoBytes = video.getBytes();
                asyncUploadService.asyncUploadVideo(river_View, videoBytes, video.getOriginalFilename());
            } catch (Exception e) {
                return AjaxResult.error("视频文件读取失败");
            }
        }

        // 异步上传封面图
        if (cover_img != null && !cover_img.isEmpty()) {
            try {
                byte[] coverBytes = cover_img.getBytes();
                asyncUploadService.asyncUploadCoverImg(river_View, coverBytes, cover_img.getOriginalFilename());
            } catch (Exception e) {
                return AjaxResult.error("封面图读取失败");
            }
        }

        //没有视频和封面
        if (river_View.getVideo() == null && river_View.getCover_img() == null) {
            riverViewService.insertRiverView(river_View);
        }

        return success("已提交上传任务，请等待完成");
    }


    /**
     * update
     * 异步更新视频和封面图
     */
    @PutMapping("/update/{id}")
    public AjaxResult edit(@PathVariable Long id,
                           @RequestParam(value = "video", required = false) MultipartFile video,
                           @RequestParam(value = "cover_img", required = false) MultipartFile cover_img) {

        try {
            RiverView up_river_View = riverViewService.selectRiverViewById(id);
            if (up_river_View == null)
                return AjaxResult.error("记录不存在");

            // 异步上传视频
            if (video != null && !video.isEmpty()) {
                byte[] videoBytes = video.getBytes();
                asyncUploadService.asyncUploadVideo(up_river_View, videoBytes, video.getOriginalFilename());
            }

            // 异步上传封面图
            if (cover_img != null && !cover_img.isEmpty()) {
                byte[] coverBytes = cover_img.getBytes();
                asyncUploadService.asyncUploadCoverImg(up_river_View, coverBytes, cover_img.getOriginalFilename());
            }

            //没有视频和封面
            if (up_river_View.getVideo() == null && up_river_View.getCover_img() == null) {
                riverViewService.insertRiverView(up_river_View);
            }

            return success("已提交上传任务，请等待完成");

        } catch (Exception e) {
            return AjaxResult.error("操作失败: " + e.getMessage());
        }
    }




    /**
     * 删除river_view
     */
    @Anonymous
    @Log(title = "river_view", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(riverViewService.deleteRiverViewByIds(ids));
    }
}
